@using System.Text.Json
@using System.Web
@using MeiwenDiaryWeb.Models
@inject SwalService _swalService

<h3>@DiaryViewModel.Year / @DiaryViewModel.Month / @DiaryViewModel.DayOfMonth</h3>

@(new MarkupString(HttpUtility.HtmlEncode(DiaryViewModel.Content).Replace(Environment.NewLine, "<br />")))

<p>
    <LinkButton Color="Color.None" OnClick="EditAsync">编辑</LinkButton>
    <PopConfirmButton ConfirmButtonColor="Color.Danger" Content="确定要删除么？"
                      CloseButtonText="关闭" ConfirmButtonText="确定"
                      OnConfirm="DeleteAsync">删除
    </PopConfirmButton>
</p>

<Modal @ref="_editModal">
    <ModalDialog ShowSaveButton="true" OnSaveAsync="SaveAsync"
                 CloseButtonText="关闭" SaveButtonText="保存">
        <BodyTemplate>
            <BootstrapInputNumber
                @bind-Value="@DiaryViewModel.Year"
                style="width: auto; display: inline-block"></BootstrapInputNumber>
            /
            <BootstrapInputNumber
                @bind-Value="@DiaryViewModel.Month"
                style="width: auto; display: inline-block"></BootstrapInputNumber>
            /
            <BootstrapInputNumber
                @bind-Value="@DiaryViewModel.DayOfMonth"
                style="width: auto; display: inline-block"></BootstrapInputNumber>
            <div style="height: 12px"></div>
            <Textarea @bind-Value="@DiaryViewModel.Content"
                      rows="10"></Textarea>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code {
    [Parameter] public DiaryViewModel DiaryViewModel { get; set; }

    private Modal _editModal;

    private async Task EditAsync() => await _editModal.Show();

    private async Task<bool> SaveAsync() {
        using var httpClient = new HttpClient();
        var jsonContent = JsonContent.Create(new { DiaryViewModel.Year, DiaryViewModel.Month, DiaryViewModel.DayOfMonth, DiaryViewModel.Content }, options: new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
        await jsonContent.ReadAsStringAsync(); // magic code to make it run but why?

        try {
            var response = await httpClient.PutAsync("http://localhost:35210/diaries/" + DiaryViewModel.Id, jsonContent);
            response.EnsureSuccessStatusCode();
        } catch (Exception e) {
            await _swalService.Show(new SwalOption { Content = e.Message, Category = SwalCategory.Error });
            return false;
        }

        await _swalService.Show(new SwalOption { Content = "日记已保存", Category = SwalCategory.Success });
        return true;
    }

    private async Task DeleteAsync() {
        using var httpClient = new HttpClient();
        try {
            var response = await httpClient.DeleteAsync("http://localhost:35210/diaries/" + DiaryViewModel.Id);
            response.EnsureSuccessStatusCode();
        } catch (Exception e) {
            await _swalService.Show(new SwalOption { Content = e.Message, Category = SwalCategory.Error });
            return;
        }

        await _swalService.Show(new SwalOption { Content = "日记已删除，请刷新页面以查看变更", Category = SwalCategory.Success });
    }

}